﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ERP.Page;
using ET;
using BLL;

namespace ERP.PageUC
{
    public partial class SectorUC : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertSector();
            }
            else
            {
                UpdateSector();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {

        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsSector = new Sector_BLL();
            var dtSector = clsSector.BindGirdSector(string.Empty);
            if (dtSector != null && dtSector.Rows.Count > 0)
            {
                foreach (DataRow dr in dtSector.Rows)
                {
                    if (txtMaNganhNghe.Text == dr["Sector_Alias"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng mã ngàng nghề !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                    else if (txtTenNganhNghe.Text == dr["Sector_Name"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng tên ngành nghề !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertSector()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Sector_ET
                    {
                        Sector_Alias = txtMaNganhNghe.Text,
                        Sector_Name = txtTenNganhNghe.Text,
                        Sector_Date_Add = DateTime.Now,
                        Sector_Date_Modify = DateTime.Now
                    };
                    Sector_BLL.InsertSector(obj);
                    ClearTextBox();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !";
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaNganhNghe.Enabled = true;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateSector()
        {
            try
            {
                var obj = new Sector_ET
                {
                    Sector_ID = int.Parse(lbID.Value),
                    Sector_Alias = txtMaNganhNghe.Text,
                    Sector_Name = txtTenNganhNghe.Text,
                    Sector_Date_Add = DateTime.Now,
                    Sector_Date_Modify = DateTime.Now
                };
                Sector_BLL.UpdateSector(obj);
                ((Sector)Page).GetSector();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaNganhNghe.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtMaNganhNghe.Text = string.Empty;
            txtTenNganhNghe.Text = string.Empty;
        }
        #endregion
    }
}